package string;

/**
 * 测试StringBuffer是否是线程安全的
 * StringBuilder线程不安全
 * @author 12609
 *
 */
public class TestStringBuffer implements Runnable{

	static TestStringBuffer instance = new TestStringBuffer();
	static StringBuffer sb = new StringBuffer();
	static StringBuilder sBuilder = new StringBuilder();
	@Override
	public void run() {
		System.out.println("开始 "+Thread.currentThread().getName());
		for(int i = 0; i < 10000; i++) {
			sb.append("A");
			sBuilder.append("B");
		}
	}

	public static void main(String[] args) {
		for(int i = 0; i < 10; i++) {
			new Thread(instance).start();
		}
		while(Thread.activeCount()>1) {
			
		}
		System.out.println("StringBuffer----理论应该是100000，实际："+sb.length());
		System.out.println("StringBuilder----理论应该是100000，实际："+sBuilder.length());
		System.out.println("finished...");
	}
}
